package com.ttmusic.view.adapters;

import java.util.ArrayList;
import java.util.List;

import com.ttmusic.activitys.R;
import com.ttmusic.core.MusicManager;
import com.ttmusic.entity.AbstractMusic;
import com.ttmusic.entity.MusicInfo;
import com.ttmusic.view.listeners.OnItemClickListener;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyLoveMusicAdapter extends MusicAdapter{
	
	private Context mContext;
	private OnItemClickListener listener;
	private List<AbstractMusic> list = new ArrayList<AbstractMusic>();

	public MyLoveMusicAdapter(Context mContext, List<AbstractMusic> list) {
		super(mContext, list, false);
    	this.mContext = mContext;
    	this.list = list;
	}
	
	@Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }
	
	public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
	
	/**
     * 
     * @param position 当前行号
     * @param convertView 当前行
     * @param parent 父级元素
     */
    @SuppressLint("InflateParams")
	@Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder mViewHolder = null;
        if (convertView == null) {
            mViewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.listitem_song, null);
            mViewHolder.song_text = (TextView) convertView.findViewById(R.id.song_title);
            mViewHolder.letter_text = (TextView) convertView.findViewById(R.id.catalog);
            mViewHolder.artist_tv = (TextView) convertView.findViewById(R.id.artist_tv);
            mViewHolder.duration_tv = (TextView) convertView.findViewById(R.id.duration_tv);
            mViewHolder.selected_view = (ImageView)convertView.findViewById(R.id.selected_view);
            mViewHolder.song_item = (LinearLayout) convertView.findViewById(R.id.song_item);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }

        MusicInfo musicInfo = (MusicInfo) getItem(position);

        mViewHolder.song_text.setText(musicInfo.getTitle());
        mViewHolder.artist_tv.setText(musicInfo.getArtist() + " - " + musicInfo.getAlbum());
        mViewHolder.duration_tv.setText(musicInfo.getDurationStr());
        mViewHolder.letter_text.setVisibility(View.GONE);
        if(MusicManager.getPlayingList() == this.list && MusicManager.getNowPlayingIndex() == position) {
        	mViewHolder.selected_view.setVisibility(View.VISIBLE);
        }else {
        	mViewHolder.selected_view.setVisibility(View.GONE);
        }
        mViewHolder.song_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(listener!=null){
                    listener.onItemClick(position);
                    notifyDataSetChanged();
                }
            }
        });
        return convertView;
    }

	public List<AbstractMusic> getList() {
		return list;
	}

	public void setList(List<AbstractMusic> list) {
		this.list = list;
	}
	final static class ViewHolder {
        TextView song_text;
        TextView letter_text;
        TextView artist_tv;
        TextView duration_tv;
        View selected_view;
        LinearLayout song_item;
    }
}
